﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data.Providers;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using DDF.Data;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
    public static partial class TempSpacesRepository
    {
        public static void UpdateIds(int rangeReviewId)
        {
            using (SqlDataProvider database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.NewCommand("TempSpaces_UpdateIds", CommandType.StoredProcedure);
                database.AddInParameter("RangeReviewId", DbType.Int32, rangeReviewId);
                database.ExecuteNonQuery();
            }
        }

        public static void TruncateTable()
        {
            using (SqlDataProvider database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.NewCommand("TRUNCATE TABLE TempSpaces ", System.Data.CommandType.Text);

                database.ExecuteNonQuery();
            }
        }

        public static void BulkCopy(DataTable spacesToCopy)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                using (SqlBulkCopy bulk = new SqlBulkCopy(conn))
                {
                    bulk.BulkCopyTimeout = 300;
                    bulk.ColumnMappings.Add("StoreCode", "StoreCode");
                    bulk.ColumnMappings.Add("StoreName", "StoreName");
                    bulk.ColumnMappings.Add("Planogram", "Planogram");
                    bulk.ColumnMappings.Add("ArticleCode", "ArticleCode");
                    bulk.ColumnMappings.Add("UOM", "UOM");
                    bulk.ColumnMappings.Add("ArticleName", "ArticleName");
                    bulk.ColumnMappings.Add("MC", "MC");
                    bulk.ColumnMappings.Add("Category", "Category");
                    bulk.ColumnMappings.Add("Facings", "Facings");
                    bulk.ColumnMappings.Add("Width", "Width");
                    bulk.DestinationTableName = "TempSpaces";
                    bulk.WriteToServer(spacesToCopy);
                    bulk.Close();
                }

                conn.Close();
            }
        }

        public static List<TempSpaceRow> SelectByIdList(int[] ids)
        {
            using (SqlDataProvider database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.NewCommand("TempSpaces_SelectByIdList", CommandType.StoredProcedure);
                database.AddInParameter("@ids", DbType.String, String.Join(",", ids));

                IDataReader reader = database.ExecuteReader();

                List<TempSpaceRow> rows = new List<TempSpaceRow>();

                Load(reader, rows);

                return rows;
            }
        }
    }
}
